<?php
!defined('P_W') && exit('Forbidden');
require_once (R_P . 'lib/base/systemdata.php');

class PW_HouseImageSource extends SystemData {
	
	/**
	 * 根据配置信息获得文章排行数据
	 * @param Array $config
	 * @param int $num
	 */
	function getSourceData($config, $num) {
		$config = $this->_initConfig($config);
		$_tmp = $this->_getData($config['sorttype'],$config['imagetype'],$num);
		$_tmp = $_tmp ? $_tmp : array();
		foreach ($_tmp as $key => $value) {
			$_tmp[$key] = $this->_cookData($value);
		}
		return $_tmp;
	}
	
	/* (non-PHPdoc)
	 * @see lib/base/SystemData#getSourceConfig()
	 */
	function getSourceConfig() {
		return array(
			'imagetype' => array(
				'name' => '图片类型',
				'type' => 'select',
				'value' => $this->_houseImageType()
			),
			'sorttype' => array(
				'name' => '图片排行',
				'type' => 'select',
				'value' => array(
					'housePic' 	=> '普通图片',
				)
			),
		);
	}
	

	
	/**
	 * 根据类型获得文章排行数据
	 * @param string $type
	 * @param int $num
	 */
	function _getData($type,$imagetype,$num) {
		$imageElementService = $this->_getHouseImageElementService();
		switch ($type) {
			case 'housePic':
				return $imageElementService->housePic($imagetype,$num);
			default:
				return array();
		}
	}
	

	/**
	 * 格式化数据统一输出
	 * @param array $data
	 * @return array
	 */
	function _cookData($data) {
		global $db_bbsurl;
		$data['url'] 	= $data['url'];
		$data['title'] 	= $data['title'];
		$data['value'] 	= $data['value'];
		$data['image']	= $data['image'];
		$data['mini']	= $data['mini'];
		$data['uploadtime']	= $data['uploadtime'];
		$data['addition'] = array();
		return $data;
	}
	
	/**
	 * @param array $config
	 * @return array
	 */
	function _initConfig($config) {
		$temp = array();
		$temp['sorttype'] = isset($config['sorttype']) ? $config['sorttype'] : '';
		$temp['imagetype'] = isset($config['imagetype']) ? $config['imagetype'] : 0;
		return $temp;
	}
	
	function _houseImageType(){
		$imageTypeService = $this->_getHouseImageTypeService();
		$topTypes = $imageTypeService->getTopTypes();
		$secendTypes = $imageTypeService->getSecendTypes();
		$imageType = array();
		foreach ($topTypes as $key => $top) {
			$name = $top['name'];
			if($secendTypes[$key]){
				foreach ($secendTypes[$key] as $k=>$secend) {
					$name .= '<option value="'.$secend['id'].'">&nbsp; &nbsp;|-'.$secend['name'].'</option>';
				}
			}
			$imageType[$top['id']] = $name;
		}
		return $imageType;
	}
	
	function _getHouseImageElementService(){
		$imageElementService = House::loadClass('imageelement'); /* @var $imageElementService PW_ImageElement */;
		return $imageElementService;
	} 
	
	function _getHouseImageTypeService(){
		$imageTypeService = House::loadClass('houseimagetype'); /* @var $imageTypeService PW_HouseImageType */;
		return $imageTypeService;
	} 
	
	function _getHouseImageService(){
		$imageService = House::loadClass('houseimage'); /* @var $imageService PW_HouseImage */;
		return $imageService;
	}
	
	
}